#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
# sys.path.append('/opt/work/caffe/python')
sys.path.insert(0, '.')

import os
import cv2

# image_path = r'/rootfs/media/kasim/Data1/dev/erisedai/out/image/10.jpg'
image_path = r'/rootfs/media/kasim/Data1/dev/erisedai/out/image1/20.jpg'


def image_encode(image, quality=None):
    out_image_name = os.path.join(os.path.dirname(image_path),
                                  'out_q{}_'.format(quality) + os.path.basename(image_path))
    if quality is None:
        cv2.imwrite(out_image_name, image)
    else:
        params = (cv2.IMWRITE_JPEG_QUALITY, quality)
        cv2.imwrite(out_image_name, image, params=params)
    os.system('chmod a+wr {}'.format(out_image_name))
    print(out_image_name, os.path.getsize(out_image_name))


def png_encode(image, quality=None):
    out_image_name = os.path.join(os.path.dirname(image_path),
                                'out_q{}_'.format(quality) + os.path.splitext(os.path.basename(image_path))[0] + '.png')
    if quality is None:
        cv2.imwrite(out_image_name, image)
    else:
        params = (cv2.IMWRITE_PNG_COMPRESSION, quality)
        cv2.imwrite(out_image_name, image, params=params)
    os.system('chmod a+wr {}'.format(out_image_name))
    print(out_image_name, os.path.getsize(out_image_name))


def main():
    image = cv2.imread(image_path)
    print(image_path, os.path.getsize(image_path))
    # image_encode(image)
    image_encode(image, quality=95)
    image_encode(image, quality=96)
    image_encode(image, quality=97)
    image_encode(image, quality=98)
    image_encode(image, quality=99)
    image_encode(image, quality=100)

    # png_encode(image)
    # png_encode(image, quality=9)
    # png_encode(image, quality=8)
    # png_encode(image, quality=7)
    # png_encode(image, quality=6)
    # png_encode(image, quality=5)
    # png_encode(image, quality=4)
    # png_encode(image, quality=3)


if __name__ == '__main__':
    main()
